package org.domain.sigepac.home;

import net.sf.trugger.reflection.Reflection;

import org.domain.sigepac.exception.ApplicationException;
import org.jboss.seam.core.Expressions;
import org.jboss.seam.core.Expressions.ValueExpression;
import org.jboss.seam.framework.EntityHome;


/**
 * Classe base para os controladores.
 *
 * @author bruno.canto
 */
public class EntityHomeSupport<E> extends EntityHome<E> {

  private static final long serialVersionUID = -8653219744731046665L;

  protected final Class<E> entityClass;

  public EntityHomeSupport() {
    entityClass = Reflection.reflect().genericType("E").in(this);
  }

  @Override
  public Class<E> getEntityClass() {
    return entityClass;
  }

  @Override
  protected E createInstance() {
    return Reflection.newInstanceOf(entityClass);
  }

  @Override
  public ValueExpression getCreatedMessage() {
    return Expressions.instance().createValueExpression("Inclus\u00e3o efetuada com sucesso.");
  }

  @Override
  public ValueExpression getUpdatedMessage() {
    return Expressions.instance().createValueExpression(
        "Altera\u00e7\u00e3o efetuada com sucesso.");
  }

  @Override
  public ValueExpression getDeletedMessage() {
    return Expressions.instance().createValueExpression("Exclus\u00e3o efetuada com sucesso.");
  }

  /**
   * Adiciona como FacesMessage o conteudo da mensagem na excecao.
   * <p>
   * Caso haja a {@link Throwable#getCause() causa} dela, sera usada como
   * parametro sua mensagem.
   *
   * @param exception
   */
  protected void addApplicationExceptionMessage(ApplicationException exception) {
    Throwable cause = exception.getCause();
    if (cause != null) {
      addFacesMessageFromResourceBundle(exception.getMessage(), cause
          .getMessage());
    } else {
      addFacesMessageFromResourceBundle(exception.getMessage());
    }
  }

}
